
import numpy as np
import cv2
import matplotlib.pyplot as plt

def load_image(image_path):
    """加载图像并转换为灰度图像"""
    image = cv2.imread(image_path)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray_image

def compute_fourier_transform(image):
    """计算图像的傅里叶变换并返回频谱图"""
    # 执行傅里叶变换
    f_transform = np.fft.fft2(image)
    f_transform_shifted = np.fft.fftshift(f_transform)  # 将零频率成分移到中心
    magnitude_spectrum = np.log(np.abs(f_transform_shifted) + 1)  # 计算幅度谱
    return magnitude_spectrum

def plot_results(original_image, magnitude_spectrum):
    """绘制原始图像和频谱图"""
    plt.figure(figsize=(12, 6))

    plt.subplot(1, 2, 1)
    plt.title("Original Image")
    plt.imshow(original_image, cmap='gray')
    plt.axis('off')

    plt.subplot(1, 2, 2)
    plt.title("Magnitude Spectrum")
    plt.imshow(magnitude_spectrum, cmap='gray')
    plt.axis('off')

    plt.show()

def main(image_path):
    """主函数"""
    # 加载图像
    image = load_image(image_path)

    # 计算傅里叶变换
    magnitude_spectrum = compute_fourier_transform(image)
    print(magnitude_spectrum.shape)
    # 绘制结果
    plot_results(image, magnitude_spectrum)

if __name__ == "__main__":
    image_path = '/home/wc/disk1/datasets/CASIAV2/image_all/Tamper/Tp/Tp_D_CRN_M_N_art10112_cha00086_11672.jpg'  # 替换为你的图像路径
    main(image_path)